

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 一、适配器模式 知识回顾 适配器定义： 将一个类的接口转换成另一种目标接口，使得原本由于接口不兼容而不能一起工作的类能一起工作适配器分类： 类结构型模式（耦合读较高） 和 对象结构型模式 两种优点：  支持调用目标接口 复用已有的类，而且无需修改原有的代码就可以重用现有的适配器类 将目标类和适配器类解藕，解决了目标类和适配器类接口不一致的问题 符合开闭原则  缺点：  适配器编写过程">
<meta property="og:type" content="article">
<meta property="og:title" content="设计模式 笔记10 _ 适配器模式 在源码中的应用 _ Spring源码的 AOP 注册中心存储了不同的适配器实现 _ SpringMVC源码doDisPatch方法里的 HandlerAdapter">
<meta property="og:url" content="http://example.com/2022/07/29/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%20%E7%AC%94%E8%AE%B010%20_%20%E9%80%82%E9%85%8D%E5%99%A8%E6%A8%A1%E5%BC%8F%20%E5%9C%A8%E6%BA%90%E7%A0%81%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8%20_%20Spring%E6%BA%90%E7%A0%81%E7%9A%84%20AOP%20%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83%E5%AD%98%E5%82%A8%E4%BA%86%E4%B8%8D%E5%90%8C%E7%9A%84%E9%80%82%E9%85%8D%E5%99%A8%E5%AE%9E%E7%8E%B0%20_%20SpringMVC%E6%BA%90%E7%A0%81doDisPatch%E6%96%B9%E6%B3%95%E9%87%8C%E7%9A%84%20HandlerAdapter/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 一、适配器模式 知识回顾 适配器定义： 将一个类的接口转换成另一种目标接口，使得原本由于接口不兼容而不能一起工作的类能一起工作适配器分类： 类结构型模式（耦合读较高） 和 对象结构型模式 两种优点：  支持调用目标接口 复用已有的类，而且无需修改原有的代码就可以重用现有的适配器类 将目标类和适配器类解藕，解决了目标类和适配器类接口不一致的问题 符合开闭原则  缺点：  适配器编写过程">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/95b3278a32134d5e85e623519887b08d.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/724348f6c5c44c6d98bc4790b5a11048.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/b368fd3271214ef2ad02727c30cf7440.png">
<meta property="article:published_time" content="2022-07-29T00:37:36.000Z">
<meta property="article:modified_time" content="2022-08-22T15:41:05.370Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="设计模式">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/95b3278a32134d5e85e623519887b08d.png">
  
  
  
  <title>设计模式 笔记10 _ 适配器模式 在源码中的应用 _ Spring源码的 AOP 注册中心存储了不同的适配器实现 _ SpringMVC源码doDisPatch方法里的 HandlerAdapter - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="设计模式 笔记10 _ 适配器模式 在源码中的应用 _ Spring源码的 AOP 注册中心存储了不同的适配器实现 _ SpringMVC源码doDisPatch方法里的 HandlerAdapter"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-07-29 08:37" pubdate>
          2022年7月29日 早上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          12k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          102 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">设计模式 笔记10 _ 适配器模式 在源码中的应用 _ Spring源码的 AOP 注册中心存储了不同的适配器实现 _ SpringMVC源码doDisPatch方法里的 HandlerAdapter</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="一、适配器模式-知识回顾"><a href="#一、适配器模式-知识回顾" class="headerlink" title="一、适配器模式 知识回顾"></a>一、适配器模式 知识回顾</h1><hr>
<p><strong>适配器定义：</strong> 将一个类的接口转换成另一种目标接口，使得原本由于接口不兼容而不能一起工作的类能一起工作<br><strong>适配器分类：</strong> 类结构型模式（耦合读较高） 和 对象结构型模式 两种<br><strong>优点：</strong></p>
<ul>
<li>支持调用目标接口</li>
<li>复用已有的类，而且无需修改原有的代码就可以重用现有的适配器类</li>
<li>将目标类和适配器类解藕，解决了目标类和适配器类接口不一致的问题</li>
<li>符合开闭原则</li>
</ul>
<p><strong>缺点：</strong></p>
<ul>
<li>适配器编写过程需结合业务场景全面考虑，可能会增加系统的复杂性</li>
<li>增加代码阅读难度，降低代码可读性，过多使用适配器会使系统代码变乱</li>
</ul>
<p><strong>主要组成：</strong></p>
<ol>
<li>目标接口 Target，当前系统业务所需要的接口，可以接口也可以是抽象类</li>
<li>适配者类 Adaptee，被访问和适配的现存组件库中的组件接口</li>
<li>适配器类  Adapter，相当于转换器，通过继承或引用 Adaptee ，把适配者接口转换成目标接口</li>
</ol>
<p><strong>模板代码：</strong><br>第一种：类适配器模式（耦合度高）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1. 目标接口</span><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">Target</span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">request</span><span class="hljs-params">()</span>;<br>&#125;<br><span class="hljs-comment">// 2. 适配者接口</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Adaptee</span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">originRequest</span><span class="hljs-params">()</span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;适配者原始的request方法&quot;</span>);<br>    &#125;<br>&#125;<br><span class="hljs-comment">// 3. 类适配器</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">ClassAdapter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Adaptee</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Target</span>&#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">request</span><span class="hljs-params">()</span> &#123;<br>        originRequest();<br>    &#125;<br>&#125;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;类适配器模式&quot;</span>);<br>        <span class="hljs-type">Target</span> <span class="hljs-variable">target</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ClassAdapter</span>();<br>        target.request();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<blockquote>
<p>运行结果<br>类适配器模式<br>适配者原始的request方法</p>
</blockquote>
<p>第二种：对象适配器</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1. 目标接口</span><br><span class="hljs-keyword">interface</span> <span class="hljs-title class_">Target</span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">request</span><span class="hljs-params">()</span>;<br>&#125;<br><span class="hljs-comment">// 2. 适配者接口</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">Adaptee</span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">originRequest</span><span class="hljs-params">()</span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;适配者原始的request方法&quot;</span>);<br>    &#125;<br>&#125;<br><span class="hljs-comment">// 3. 对象适配器</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">ObjectAdapter</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Target</span>&#123;<br>    <span class="hljs-keyword">private</span> Adaptee adaptee;<br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">ObjectAdapter</span><span class="hljs-params">(Adaptee adaptee)</span>&#123;<br>        <span class="hljs-built_in">this</span>.adaptee = adaptee;<br>    &#125;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">request</span><span class="hljs-params">()</span> &#123;<br>        adaptee.originRequest();<br>    &#125;<br>&#125;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        System.out.println(<span class="hljs-string">&quot;对象适配器模式&quot;</span>);<br>        <span class="hljs-type">Adaptee</span> <span class="hljs-variable">adaptee</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Adaptee</span>();<br>        <span class="hljs-type">Target</span> <span class="hljs-variable">target</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectAdapter</span>(adaptee);<br>        target.request();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<blockquote>
<p>运行结果<br>对象适配器模式<br>适配者原始的request方法</p>
</blockquote>
<p><strong>适配器模式的使用场景：</strong></p>
<ul>
<li>以前开发的系统存在满足新功能需求的类，但其接口同新系统的接口不一致</li>
<li>使用第三方提供的组件，但接口定义和自己要求的接口定义不同</li>
</ul>
<h1 id="二、类适配器使用案例之拓展登陆业务"><a href="#二、类适配器使用案例之拓展登陆业务" class="headerlink" title="二、类适配器使用案例之拓展登陆业务"></a>二、类适配器使用案例之拓展登陆业务</h1><hr>
<p>在使用 JavaWeb 开发时，几乎都离不开注册、登陆的业务，比如 CSDN，我们可以使用 账号密码登陆，也可以使用微信登陆、手机号登陆等，虽然登陆的形式不同，但其实登陆后的逻辑是相同的，都是将登陆状态保存到 Token 或者 Session。</p>
<p>接下来我们就通过一个实例来体会类适配器的用法，以一个旧版的登陆业务为例：</p>
<h2 id="2-1-模拟登陆的旧系统"><a href="#2-1-模拟登陆的旧系统" class="headerlink" title="2.1 模拟登陆的旧系统"></a>2.1 模拟登陆的旧系统</h2><ol>
<li><p>前后端交互类 R.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.entity;<br><br><span class="hljs-keyword">import</span> lombok.AllArgsConstructor;<br><span class="hljs-keyword">import</span> lombok.Data;<br><span class="hljs-keyword">import</span> lombok.NoArgsConstructor;<br><br><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@AllArgsConstructor</span><br><span class="hljs-meta">@NoArgsConstructor</span><br><span class="hljs-keyword">public</span><br><span class="hljs-comment">// 后端返回给前端的对象</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">R</span>&lt;T&gt;&#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-type">int</span> code;<br>    <span class="hljs-keyword">private</span> String msg;<br>    <span class="hljs-keyword">private</span> T data;<br>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li><p>用户实体类 User.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni.entity;<br><br><span class="hljs-keyword">import</span> lombok.Data;<br><br><span class="hljs-meta">@Data</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">User</span> &#123;<br>    <span class="hljs-keyword">private</span> String username;<br>    <span class="hljs-keyword">private</span> String password;<br>&#125;<br><br></code></pre></td></tr></table></figure>
</li>
<li><p>用户注册、登陆业务逻辑层 UserService.java （接口省略了）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.uni;<br><br><span class="hljs-keyword">import</span> com.uni.entity.R;<br><span class="hljs-keyword">import</span> com.uni.entity.User;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserService</span> &#123;<br>    <span class="hljs-comment">// 注册方法</span><br>    <span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">register</span><span class="hljs-params">(User user)</span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">R</span>&lt;User&gt;(<span class="hljs-number">200</span>, <span class="hljs-string">&quot;注册成功&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>());<br>    &#125;<br>    <span class="hljs-comment">// 登录方法</span><br>    <span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">login</span><span class="hljs-params">(User user)</span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">R</span>&lt;User&gt; (<span class="hljs-number">200</span>, <span class="hljs-string">&quot;登陆成功&quot;</span>, user);<br>    &#125;<br><br>&#125;<br></code></pre></td></tr></table></figure></li>
</ol>
<p>上面旧系统中的三个方法是能实现用户注册与登陆的稳定代码（仅作示例，细节忽略）</p>
<h2 id="2-2-旧系统拓展【类适配器】"><a href="#2-2-旧系统拓展【类适配器】" class="headerlink" title="2.2 旧系统拓展【类适配器】"></a>2.2 旧系统拓展【类适配器】</h2><p>接下来我们使用类适配器模式实现不修改原有的代码来进行功能的拓展，比如需要实现 QQ登陆、微信登陆、手机号登陆。（假设拓展后的系统为 V1版本）<br>第一步，编写目标接口，即拓展的业务接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">IUserServiceV1</span>&#123;<br>    R&lt;User&gt; <span class="hljs-title function_">loginForQQ</span><span class="hljs-params">(String api)</span>;<br>    R&lt;User&gt; <span class="hljs-title function_">loginForWechat</span><span class="hljs-params">(String api)</span>;<br>    R&lt;User&gt; <span class="hljs-title function_">loginForTelphone</span><span class="hljs-params">(String phone, String code)</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>第二步，增加适配者类，继承原有的逻辑，并实现上方的目标接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> service.impl;<br><br><span class="hljs-keyword">import</span> com.uni.entity.R;<br><span class="hljs-keyword">import</span> com.uni.entity.User;<br><span class="hljs-keyword">import</span> service.IUserServiceV1;<br><span class="hljs-keyword">import</span> service.UserService;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserServiceV1Adapter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">UserService</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">IUserServiceV1</span> &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">loginForQQ</span><span class="hljs-params">(String api)</span> &#123;<br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>();<br>        <span class="hljs-comment">// 根据 api 获取用户对象...</span><br>        <span class="hljs-keyword">return</span> login(user);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">loginForWechat</span><span class="hljs-params">(String api)</span> &#123;<br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>();<br>        <span class="hljs-comment">// 根据 api 获取用户对象...</span><br>        <span class="hljs-keyword">return</span> login(user);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">loginForTelphone</span><span class="hljs-params">(String phone, String code)</span> &#123;<br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>();<br>        <span class="hljs-comment">// 验证手机号, 并获取用户对象</span><br>        <span class="hljs-keyword">return</span> login(user);<br>    &#125;<br><br>    <span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">login</span><span class="hljs-params">(User user)</span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>.login(user);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>测试：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">IUserServiceV1</span> <span class="hljs-variable">service</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">UserServiceV1Adapter</span>();<br>        R&lt;User&gt; info1 = service.loginForQQ(<span class="hljs-string">&quot;QQ登陆请求成功的api&quot;</span>);<br>        R&lt;User&gt; info2 = service.loginForTelphone(<span class="hljs-string">&quot;登陆的手机号&quot;</span>, <span class="hljs-string">&quot;接收到的验证码&quot;</span>);<br>        System.out.println(<span class="hljs-string">&quot;QQ登录: &quot;</span> +  info1);<br>        System.out.println(<span class="hljs-string">&quot;手机号登陆: &quot;</span> + info2);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>


<h2 id="2-3-旧系统拓展【接口适配器】"><a href="#2-3-旧系统拓展【接口适配器】" class="headerlink" title="2.3 旧系统拓展【接口适配器】"></a>2.3 旧系统拓展【接口适配器】</h2><p>之前的拓展方式直接继承了原先的 UserService，可以看出耦合度比较高，现可以换一种方式，先定义一个适配器的接口，然后使用抽象类实现，最后不同登陆方式继承抽象类即可<br>第一步，定义适配器接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> service;<br><br><span class="hljs-keyword">import</span> com.uni.entity.R;<br><span class="hljs-keyword">import</span> com.uni.entity.User;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">IUserServiceAdapter</span> &#123;<br>    <span class="hljs-type">boolean</span> <span class="hljs-title function_">support</span><span class="hljs-params">(Object adapter)</span>;<br>    R&lt;User&gt; <span class="hljs-title function_">login</span> <span class="hljs-params">(Object loginInfo, Object adapter)</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>第二步，定义适配器抽象类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> service;<br><br><span class="hljs-keyword">import</span> com.uni.entity.R;<br><span class="hljs-keyword">import</span> com.uni.entity.User;<br><br><span class="hljs-keyword">public</span>  <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">AbstractAdapter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">UserService</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">IUserServiceAdapter</span>&#123;<br>    <span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">loginOrigin</span><span class="hljs-params">(User user)</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>.login(user);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>第三步，定义需要的适配器<br>QQ业务适配器类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> service;<br><br><span class="hljs-keyword">import</span> com.uni.entity.R;<br><span class="hljs-keyword">import</span> com.uni.entity.User;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserServiceQQAdapter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AbstractAdapter</span>&#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">support</span><span class="hljs-params">(Object adapter)</span> &#123;<br>        <span class="hljs-keyword">return</span> adapter <span class="hljs-keyword">instanceof</span> UserServiceQQAdapter;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">login</span><span class="hljs-params">(Object loginInfo, Object adapter)</span> &#123;<br>        <span class="hljs-keyword">if</span> (!support(adapter))<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">R</span>&lt;User&gt;(<span class="hljs-number">500</span>, <span class="hljs-string">&quot;登陆失败&quot;</span>, <span class="hljs-literal">null</span>);<br>        <span class="hljs-comment">// 根据 loginInfo 获取 User</span><br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>();<br>        <span class="hljs-comment">// 执行原始的登录</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>.loginOrigin(user);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>微信业务适配器类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> service;<br><br><span class="hljs-keyword">import</span> com.uni.entity.R;<br><span class="hljs-keyword">import</span> com.uni.entity.User;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserServiceWeiXinAdapter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AbstractAdapter</span>&#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-type">boolean</span> <span class="hljs-title function_">support</span><span class="hljs-params">(Object adapter)</span> &#123;<br>        <span class="hljs-keyword">return</span> adapter <span class="hljs-keyword">instanceof</span> UserServiceWeiXinAdapter;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> R&lt;User&gt; <span class="hljs-title function_">login</span><span class="hljs-params">(Object loginInfo, Object adapter)</span> &#123;<br>        <span class="hljs-keyword">if</span>(!support(adapter))&#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">R</span>&lt;User&gt;(<span class="hljs-number">500</span>, <span class="hljs-string">&quot;适配器不匹配&quot;</span>, <span class="hljs-literal">null</span>);<br>        &#125;<br>        <span class="hljs-comment">// 根据微信登陆的 api 获取用户信息</span><br>        <span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">User</span>();<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>.loginOrigin(user);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>测试：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.uni.entity.R;<br><span class="hljs-keyword">import</span> com.uni.entity.User;<br><span class="hljs-keyword">import</span> service.IUserServiceAdapter;<br><span class="hljs-keyword">import</span> service.UserServiceQQAdapter;<br><span class="hljs-keyword">import</span> service.UserServiceWeiXinAdapter;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span>&#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">IUserServiceAdapter</span> <span class="hljs-variable">adapter1</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">UserServiceQQAdapter</span>();<br>        <span class="hljs-type">IUserServiceAdapter</span> <span class="hljs-variable">adapter2</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">UserServiceWeiXinAdapter</span>();<br>        R&lt;User&gt; r1 = adapter1.login(<span class="hljs-string">&quot;QQ登陆的API&quot;</span>, adapter1);<br>        R&lt;User&gt; r2 = adapter2.login(<span class="hljs-string">&quot;WX登陆的API&quot;</span>, adapter2);<br>        System.out.println(<span class="hljs-string">&quot;qq登陆结果: &quot;</span> +r1);<br>        System.out.println(<span class="hljs-string">&quot;wx登陆结果: &quot;</span> + r2);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>类图关系如下：<br><img src="https://img-blog.csdnimg.cn/95b3278a32134d5e85e623519887b08d.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="三、适配器模式在Spring源码中的应用"><a href="#三、适配器模式在Spring源码中的应用" class="headerlink" title="三、适配器模式在Spring源码中的应用"></a>三、适配器模式在Spring源码中的应用</h1><hr>
<p>Spring版本 V5.3.21在Spring 的 AOP 面向切面编程 中，适配器模式运用的非常广泛。<br>Spring 提供了<code>org.springframework.aop.framework.adapterDefaultAdvisorAdapterRegistry</code> 类 专门用于注册不同的适配器</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> org.springframework.aop.framework.adapter;<br><span class="hljs-comment">// 默认的通知适配器注册中心</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">DefaultAdvisorAdapterRegistry</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">AdvisorAdapterRegistry</span>, Serializable &#123;<br>    <span class="hljs-comment">// 存储 AdvisorAdapter 的列表，只有三个</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> List&lt;AdvisorAdapter&gt; adapters = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayList</span>(<span class="hljs-number">3</span>);<br>    <span class="hljs-comment">// 无参构造</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-title function_">DefaultAdvisorAdapterRegistry</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">// 注册三种不同的 Advice 通知适配器</span><br>        <span class="hljs-built_in">this</span>.registerAdvisorAdapter(<span class="hljs-keyword">new</span> <span class="hljs-title class_">MethodBeforeAdviceAdapter</span>());<br>        <span class="hljs-built_in">this</span>.registerAdvisorAdapter(<span class="hljs-keyword">new</span> <span class="hljs-title class_">AfterReturningAdviceAdapter</span>());<br>        <span class="hljs-built_in">this</span>.registerAdvisorAdapter(<span class="hljs-keyword">new</span> <span class="hljs-title class_">ThrowsAdviceAdapter</span>());<br>    &#125;<br>    <span class="hljs-comment">// 获取 Advisor 接口</span><br>    <span class="hljs-keyword">public</span> Advisor <span class="hljs-title function_">wrap</span><span class="hljs-params">(Object adviceObject)</span> <span class="hljs-keyword">throws</span> UnknownAdviceTypeException &#123; &#125;<br><br>    <span class="hljs-comment">// 获取 Advisor 接口所有的拦截器</span><br>    <span class="hljs-keyword">public</span> MethodInterceptor[] getInterceptors(Advisor advisor) <span class="hljs-keyword">throws</span> UnknownAdviceTypeException &#123;<br>     <br>    &#125;<br>    <span class="hljs-comment">// 注册 Adapter 适配器</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">registerAdvisorAdapter</span><span class="hljs-params">(AdvisorAdapter adapter)</span> &#123;<br>        <span class="hljs-built_in">this</span>.adapters.add(adapter);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>比如 Spring 支持使用 <code>Advice（通知）</code>来增强被代理类的功能，在Spring 5.3.21 版本中，Advice的类型主要有 BeforeAdvice、AfterAdvice、DynamicIntroductionAdvice 这三种，其中还有一些继承的子接口，如下图：<br><img src="https://img-blog.csdnimg.cn/724348f6c5c44c6d98bc4790b5a11048.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>对于这么多的 Advice 接口，Spring 底层提供了不同的 Adapter 适配器对其进行管理。主要看 <code>DefaultAdvisorAdapterRegistry</code>类就知道了。</p>
<p>其内部使用 List 存储三种不同的 Advice 适配器类型，最终都存储为AdvisorAdapter 接口。Spring 会根据不同的 AOP 配置来确定使用对应的 Advice。</p>
<h1 id="四、SpringMVC中的适配器模式"><a href="#四、SpringMVC中的适配器模式" class="headerlink" title="四、SpringMVC中的适配器模式"></a>四、SpringMVC中的适配器模式</h1><p>适配器在 SpringMVC 中的经典使用体现在它的核心方法 doDispatch 中，接下来先以 HandlerAdapter 类为主：</p>
<p><img src="https://img-blog.csdnimg.cn/b368fd3271214ef2ad02727c30cf7440.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>这些适配调用的关键代码在 DispatcherServlet类的 doDispatch() 方法中：</p>
<p>doDispatch()  只在 doService() 方法中调用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> org.springframework.web.servlet;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">DispatcherServlet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">FrameworkServlet</span> &#123;<br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">doDispatch</span><span class="hljs-params">(HttpServletRequest request, HttpServletResponse response)</span> <span class="hljs-keyword">throws</span> Exception &#123;<br>        <span class="hljs-comment">// 存储当前的请求</span><br>        <span class="hljs-type">HttpServletRequest</span> <span class="hljs-variable">processedRequest</span> <span class="hljs-operator">=</span> request;<br>        <span class="hljs-comment">// 初始化处理器</span><br>		<span class="hljs-type">HandlerExecutionChain</span> <span class="hljs-variable">mappedHandler</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br>        <span class="hljs-comment">// 标记是否为文件处理的请求</span><br>		<span class="hljs-type">boolean</span> <span class="hljs-variable">multipartRequestParsed</span> <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>;<br>        <span class="hljs-comment">// 获取同步请求的管理器</span><br>		<span class="hljs-type">WebAsyncManager</span> <span class="hljs-variable">asyncManager</span> <span class="hljs-operator">=</span> WebAsyncUtils.getAsyncManager(request);<br><br>		<span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">// 初始化视图模型</span><br>			<span class="hljs-type">ModelAndView</span> <span class="hljs-variable">mv</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br>            <span class="hljs-comment">// 初始化方法执行的异常</span><br>			<span class="hljs-type">Exception</span> <span class="hljs-variable">dispatchException</span> <span class="hljs-operator">=</span> <span class="hljs-literal">null</span>;<br><br>			<span class="hljs-keyword">try</span> &#123;<br>                <span class="hljs-comment">// 检查是否为文件处理的请求</span><br>				processedRequest = checkMultipart(request);<br>                <span class="hljs-comment">// 如果处理后的processes请求和之前不同，那么则标记为文件请求</span><br>				multipartRequestParsed = (processedRequest != request);<br><br>				<span class="hljs-comment">// Determine handler for the current request.</span><br>                <span class="hljs-comment">// 获取当前请求对应的 Handler 处理器</span><br>				mappedHandler = getHandler(processedRequest);<br>				<span class="hljs-keyword">if</span> (mappedHandler == <span class="hljs-literal">null</span>) &#123;	<span class="hljs-comment">// 处理器为空，跑出异常</span><br>					noHandlerFound(processedRequest, response);<br>					<span class="hljs-keyword">return</span>;<br>				&#125;<br>                <br>				<span class="hljs-comment">// Determine handler adapter for the current request.</span><br>                <span class="hljs-comment">// 获取当前请求对应的 Handler 适配器</span><br>				<span class="hljs-type">HandlerAdapter</span> <span class="hljs-variable">ha</span> <span class="hljs-operator">=</span> getHandlerAdapter(mappedHandler.getHandler());<br>                    <br>				<span class="hljs-comment">// Process last-modified header, if supported by the handler.</span><br>                <span class="hljs-comment">// 获取请求的方法</span><br>				<span class="hljs-type">String</span> <span class="hljs-variable">method</span> <span class="hljs-operator">=</span> request.getMethod();<br>                <span class="hljs-comment">// 判断是否为 GET 请求</span><br>				<span class="hljs-type">boolean</span> <span class="hljs-variable">isGet</span> <span class="hljs-operator">=</span> HttpMethod.GET.matches(method);<br>                <span class="hljs-comment">// 若为 GET 请求 且带有请求头</span><br>				<span class="hljs-keyword">if</span> (isGet || HttpMethod.HEAD.matches(method)) &#123;<br>                    <span class="hljs-comment">// 调整请求头</span><br>					<span class="hljs-type">long</span> <span class="hljs-variable">lastModified</span> <span class="hljs-operator">=</span> ha.getLastModified(request, mappedHandler.getHandler());<br>					<span class="hljs-keyword">if</span> (<span class="hljs-keyword">new</span> <span class="hljs-title class_">ServletWebRequest</span>(request, response).checkNotModified(lastModified) &amp;&amp; isGet) &#123;<br>						<span class="hljs-keyword">return</span>;<br>					&#125;<br>				&#125;<br>                <br>				<span class="hljs-keyword">if</span> (!mappedHandler.applyPreHandle(processedRequest, response)) &#123;<br>					<span class="hljs-keyword">return</span>;<br>				&#125;<br><br>				<span class="hljs-comment">// Actually invoke the handler.</span><br>                <span class="hljs-comment">// 调用真正的处理器方法</span><br>				mv = ha.handle(processedRequest, response, mappedHandler.getHandler());<br>                <span class="hljs-comment">// 若已执行就返回, 防止重复执行</span><br>				<span class="hljs-keyword">if</span> (asyncManager.isConcurrentHandlingStarted()) &#123;<br>					<span class="hljs-keyword">return</span>;<br>				&#125;<br>               <span class="hljs-comment">// 提交默认的视图名称</span><br>				applyDefaultViewName(processedRequest, mv);<br>                <span class="hljs-comment">// 提交 POST的请求处理</span><br>				mappedHandler.applyPostHandle(processedRequest, response, mv);<br>			&#125;<br>			<span class="hljs-keyword">catch</span> (Exception ex) &#123;<br>				dispatchException = ex;<br>			&#125;<br>			<span class="hljs-keyword">catch</span> (Throwable err) &#123;<br>				<span class="hljs-comment">// As of 4.3, we&#x27;re processing Errors thrown from handler methods as well,</span><br>				<span class="hljs-comment">// making them available for @ExceptionHandler methods and other scenarios.</span><br>				dispatchException = <span class="hljs-keyword">new</span> <span class="hljs-title class_">NestedServletException</span>(<span class="hljs-string">&quot;Handler dispatch failed&quot;</span>, err);<br>			&#125;<br>            <span class="hljs-comment">// 处理请求的结果</span><br>			processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);<br>		&#125;<br>		<span class="hljs-keyword">catch</span> (Exception ex) &#123;<br>			triggerAfterCompletion(processedRequest, response, mappedHandler, ex);<br>		&#125;<br>		<span class="hljs-keyword">catch</span> (Throwable err) &#123;<br>			triggerAfterCompletion(processedRequest, response, mappedHandler,<br>					<span class="hljs-keyword">new</span> <span class="hljs-title class_">NestedServletException</span>(<span class="hljs-string">&quot;Handler processing failed&quot;</span>, err));<br>		&#125;<br>		<span class="hljs-keyword">finally</span> &#123;<br>			<span class="hljs-keyword">if</span> (asyncManager.isConcurrentHandlingStarted()) &#123;<br>				<span class="hljs-comment">// Instead of postHandle and afterCompletion</span><br>				<span class="hljs-keyword">if</span> (mappedHandler != <span class="hljs-literal">null</span>) &#123;<br>                    <span class="hljs-comment">// 提交结果</span><br>					mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);<br>				&#125;<br>			&#125;<br>			<span class="hljs-keyword">else</span> &#123;<br>				<span class="hljs-comment">// Clean up any resources used by a multipart request.</span><br>				<span class="hljs-keyword">if</span> (multipartRequestParsed) &#123;<br>					cleanupMultipart(processedRequest);<br>				&#125;<br>			&#125;<br>		&#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>在上面 doDispatch () 方法的源码中，用到适配器的部分为：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 根据 Handler 获取对应的适配器</span><br><span class="hljs-type">HandlerAdapter</span> <span class="hljs-variable">ha</span> <span class="hljs-operator">=</span> getHandlerAdapter(mappedHandler.getHandler());<br><span class="hljs-comment">// 调用适配器的 handle 方法处理请求</span><br>mv = ha.handle(processedRequest, response, mappedHandler.getHandler());<br></code></pre></td></tr></table></figure>

<p>接下来再看看 getHandlerAdapter()方法的源码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> org.springframework.web.servlet;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">DispatcherServlet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">FrameworkServlet</span> &#123;<br>    <span class="hljs-meta">@Nullable</span><br>    <span class="hljs-keyword">private</span> List&lt;HandlerAdapter&gt; handlerAdapters;<br>    <br>    <span class="hljs-keyword">protected</span> HandlerAdapter <span class="hljs-title function_">getHandlerAdapter</span><span class="hljs-params">(Object handler)</span> <span class="hljs-keyword">throws</span> ServletException &#123;<br>        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.handlerAdapters != <span class="hljs-literal">null</span>) &#123;<br>            <span class="hljs-comment">// 遍历当前类支持的所有适配器</span><br>            <span class="hljs-keyword">for</span> (HandlerAdapter adapter : <span class="hljs-built_in">this</span>.handlerAdapters) &#123;<br>                <span class="hljs-comment">// 如果适配器符合，则直接返回</span><br>                <span class="hljs-keyword">if</span> (adapter.supports(handler)) &#123;<br>                    <span class="hljs-keyword">return</span> adapter;<br>                &#125;<br>            &#125;<br>        &#125;<br>        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ServletException</span>(<span class="hljs-string">&quot;No adapter for handler [&quot;</span> + handler +<br>                                   <span class="hljs-string">&quot;]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>通过源码可以看到，在 DispatcherServlet 类里定义了存储 Handler 适配器的列表，并提供了 getHandlerAdapter() 方法来获取 Handler 对应的适配器，判断适配器是否兼容则调用适配器接口提供的 supports 方法。</p>
<h1 id="五、总结"><a href="#五、总结" class="headerlink" title="五、总结"></a>五、总结</h1><hr>
<p>适配器模式支持在原有的系统上拓展接口，分为类适配器和对象&#x2F;接口适配器两种。</p>
<p>适配器通常带有 Adapter 的标识。</p>
<p>在 Spring AOP 中使用到了适配器，不同的 Advice 通知接口有着不同的 AdviceAdapter 适配器接口，Spring 提供了 DefaultAdvisorAdapterRegistry 类来负责注册 AdvisorAdapter，<br>该类初始化就注册了三种不同的 AdviceAdapter 适配器实现类，分别是方法执行前、方法执行后返回前和抛出异常前的 AdviceAdatper。同时提供 wrap 方法，可以获取Advice 对应的接口。</p>
<p>在 SpringMVC 中，处理 Controller 层的方法就用到了 HandlerAdapter 适配器，Controller在请求后，会来到 DispatcherServlet类的 doService 方法 ，然后再调用 doDispatch 方法，在该方法中用到 适配器的方法其实就两个，第一个是获取对应的适配器 getHandlerAdapter，第二个就是调用适配器的 handle方法，根据 请求、响应 以及 Handler 来渲染视图模型。</p>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><hr>
<p>[1] <a target="_blank" rel="noopener" href="http://c.biancheng.net/view/1361.html">http://c.biancheng.net/view/1361.html</a><br><a target="_blank" rel="noopener" href="http://c.biancheng.net/view/vip_8439.html">
</a></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" class="category-chain-item">设计模式</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">#设计模式</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>设计模式 笔记10 _ 适配器模式 在源码中的应用 _ Spring源码的 AOP 注册中心存储了不同的适配器实现 _ SpringMVC源码doDisPatch方法里的 HandlerAdapter</div>
      <div>http://example.com/2022/07/29/设计模式 笔记10 _ 适配器模式 在源码中的应用 _ Spring源码的 AOP 注册中心存储了不同的适配器实现 _ SpringMVC源码doDisPatch方法里的 HandlerAdapter/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年7月29日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/07/29/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%20%E7%AC%94%E8%AE%B01%20_%20%E4%BA%94%E4%B8%AA%E5%88%9B%E5%BB%BA%E5%9E%8B%E6%A8%A1%E5%BC%8F%20_%20Java%20%E8%AF%AD%E8%A8%80%E5%AE%9E%E7%8E%B0%20_%20%E5%B7%A5%E5%8E%82%E6%96%B9%E6%B3%95%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%20_%20%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%20_%E5%BB%BA%E9%80%A0%E8%80%85%E6%A8%A1%E5%BC%8F%20_%20%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F%20_%20%E5%8E%9F%E5%9E%8B%E6%A8%A1%E5%BC%8F/" title="设计模式 笔记1 _ 五个创建型模式 _ Java 语言实现 _ 工厂方法设计模式 _ 抽象工厂模式 _建造者模式 _ 单例模式 _ 原型模式">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">设计模式 笔记1 _ 五个创建型模式 _ Java 语言实现 _ 工厂方法设计模式 _ 抽象工厂模式 _建造者模式 _ 单例模式 _ 原型模式</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/07/28/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%20%E7%AC%94%E8%AE%B09%20_%20%E5%8E%9F%E5%9E%8B%E6%A8%A1%E5%BC%8F%20%E5%9C%A8%E6%BA%90%E7%A0%81%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8%20_%20ArrayList%20%E4%B8%AD%E7%9A%84%E5%8E%9F%E5%9E%8B%E6%A8%A1%E5%BC%8F%20_%20clone%E6%B5%85%E5%85%8B%E9%9A%86%20_%20%E5%9F%BA%E4%BA%8E%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%B5%81%E7%9A%84%E6%B7%B1%E5%85%8B%E9%9A%86%E5%AE%9E%E7%8E%B0/" title="设计模式 笔记9 _ 原型模式 在源码中的应用 _ ArrayList 中的原型模式 _ clone浅克隆 _ 基于二进制流的深克隆实现">
                        <span class="hidden-mobile">设计模式 笔记9 _ 原型模式 在源码中的应用 _ ArrayList 中的原型模式 _ clone浅克隆 _ 基于二进制流的深克隆实现</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
